﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Categories.aspx.cs" Inherits="FalconSystemCatalogWebsite.Pages.Categories" %>

<!DOCTYPE html>
<link href="../Css/FalconSystemCatalogStyles.css" rel="stylesheet" />
<link href="../Css/metro-bootstrap.css" rel="stylesheet" />

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
        <script type="text/javascript">

            function SearchBox_OnKeyPress() {
                var txtSearchBox = document.getElementById("txtSearchBox");
                var treeList = $find('<%= CategoriesRadTreeView.ClientID %>');

               
            for (var i = 0; i < treeList.get_nodes().get_count() ; i++) {
                findNodes(treeList.get_nodes().getNode(i), txtSearchBox.value);
            }
        }

            function findNodes(node, searchString) {
                var hasFoundChildren = false;

                if (hasFoundChildren || node.get_text().toLowerCase().indexOf(searchString.toLowerCase()) != -1) {
                    node.set_selected(true);
                }

                for (var i = 0; i < node.get_nodes().get_count() ; i++) {
                    hasFoundChildren = findNodes(node.get_nodes().getNode(i), searchString) || hasFoundChildren;
                }

                if (hasFoundChildren || node.get_text().toLowerCase().indexOf(searchString.toLowerCase()) != -1) {
                    node.set_visible(true);

                    node.expand();
                    if (node.get_text().toLowerCase().indexOf(searchString.toLowerCase()) != -1) {
                        node.set_selected(true);
                        for (var i = 0; i < node.get_nodes().get_count() ; i++) {
                            DisplayChildren(node);
                            //node.get_nodes().getNode(i).set_visible(true);
                        }
                    }
                    return true;
                }
                else {
                    node.set_visible(false);
                    return false;
                }
            }

            function DisplayChildren(node) {
                for (var ee = 0; ee < node.get_nodes().get_count() ; ee++) {
                    node.get_nodes().getNode(ee).set_visible(true);
                    //node.get_nodes().getNode(ee).set_expanded(true);
                    DisplayChildren(node.get_nodes().getNode(ee));
                }
            }

        </script>
    </telerik:RadScriptBlock>
</head>
<body>
    <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
        <div>
            <div class="metro">
                <div class="input-control text">
                    <input id="txtSearchBox" onkeyup="SearchBox_OnKeyPress()" type="text" placeholder="Buscador ..." autocomplete="off" />
                    <button class="btn-clear" onclick="SearchBox_OnKeyPress()"></button>
                </div>
            </div>
            <telerik:RadTreeView ID="CategoriesRadTreeView" runat="server"></telerik:RadTreeView>
        </div>
    </form>
</body>
</html>
